<?php
namespace app\index\controller;
use app\index\model\AuthGroup as AuthGroupModel;
use app\index\controller\Common;
/**
*y用户组控制器
*   @author 于江 2017年3月30日
*   邮箱935315164@qq.com
*/
class AuthGroup extends Common
{
	// 列表页
	public function index(){
		$AuthGroup=AuthGroupModel::paginate(10);
		$this->assign('AuthGroup',$AuthGroup);
		return $this->fetch();
	}

	// 添加
	public function add(){
		if (request()->isPost()) {
			$data=input('post.');
			if ($data['rules']) {//判断规则是否存在
				$data['rules']=implode(',', $data['rules']);
			}
			$_data=array();//数组重组
			foreach ($data as $k => $v) {
				$_data[]=$k;//健值压入数组
			}
			if (!in_array('status', $_data)) {
				$data['status']=0;
			}
			$add=db('auth_group')->insert($data);
			 $validate = \think\Loader::validate('AuthGroup');
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }
			if ($add) {
				$this->success('添加用户组成功！',url('index'));
			}else{
				$this->error('添加用户组失败！');
			}
		}
		$authRule=new \app\index\model\AuthRule();//权限模型
		$authRuleRes=$authRule->AuthRuleTree();
		$this->assign('authRuleRes',$authRuleRes);
		return $this->fetch();
	}
	// 编辑
	public function edit(){
		if (request()->isPost()) {
			$data=input('post.');
			if ($data['rules']) {//判断规则是否存在
				$data['rules']=implode(',', $data['rules']);
			}
			$_data=array();//数组重组
			foreach ($data as $k => $v) {
				$_data[]=$k;//健值压入数组
			}
			if (!in_array('status', $_data)) {
				$data['status']=0;
			}

			$save=db('auth_group')->update($data);
			$validate = \think\Loader::validate('AuthGroup');
            if(!$validate->scene('edit')->check($data)){
                $this->error($validate->getError());
            }
			if ($save!==false) {
				$this->success('修改用户组成功！',url('index'));
			}else{
				$this->error('修改用户组失败！');
			}
		}
		$authres=db('auth_group')->find(input('id'));
		$this->assign('authres',$authres);
		$authRule=new \app\index\model\AuthRule();//权限模型
		$authRuleRes=$authRule->AuthRuleTree();
		$this->assign('authRuleRes',$authRuleRes);
		return $this->fetch();
	}
	// 删除
	public function del(){
		$del=db('auth_group')->delete(input('id'));
		if ($del) {
			echo "1";
			
		}else{
			echo "0";
		
		}
	}







}
